from viewgprprofiles import Viewer


def process():
    
    ''' First step is to get a list of all folders and files in the raw
    data directory '''
    import os
    import sys
    from PyQt4 import QtGui
    
    rawdir = 'F:\\IPYdata\\CBAND'
    extfilter = '''Raw Files (*.gYY);;TD Files (*.bYY);;All Files (*.*)'''
    folder = '080107'
    
    app = QtGui.QApplication(sys.argv)
    dlg = QtGui.QFileDialog
    pathlist = dlg.getOpenFileNames(None, "Open File", rawdir+'\\'+folder, extfilter)
    pathlist.sort()
    filelist = []
    for path in pathlist:
        filelist.append(os.path.basename(str(path)))
    os.chdir(os.path.dirname(str(pathlist[0])))      
    
    ''' Edit and store every single file '''
    VU = Viewer(folder)
    VU.connect()
    VU.getList(filelist)
    VU.show()
     
    
    
def merge():
    
    ''' First step is to get a list of all folders and files in the raw
    data directory '''
    import os
    procdir = 'F:\\IPYdata\\CBAND\\processed\\NUS07-5-7'
    os.chdir(procdir)
    filelist = os.listdir(os.getcwd())
    filelist.sort()
    print filelist
    section = mergeProfiles(filelist, nstack=40)
    section = section.astype(np.float32)
    section.tofile('NUS07-5-7.bYY')
#    plt.imshow(section, aspect=5)
#    plt.show()            
             
                
def PDFprint():
    import os
    import matplotlib as mpl
    
    ''' Customize RC settings '''
    mpl.rcParams['backend'] = 'PDF'
    mpl.rcParams['figure.subplot.left'] =  0.02  
    mpl.rcParams['figure.subplot.right'] =  0.98    
    mpl.rcParams['figure.subplot.bottom'] =  0.05   
    mpl.rcParams['figure.subplot.top'] =  0.95 
        
    procdir = 'F:\\IPYdata\\CBAND\\sections'
    filename = 'NUS07-1-2.bYY'
    os.chdir(procdir)
    TD = TGPR()
    TD.readBin(filename, 805)
    DPI = 110
    S = np.shape(TD.Tdata)
    W = S[1]/DPI
    H = 33.1
    
    plt.figure(num=1, figsize=(W, H), dpi=DPI, facecolor='w', edgecolor='w')
    plt.imshow(TD.Tdata, aspect=2, cmap=plt.cm.binary, vmin=122, vmax=142)
    plt.title(filename[:-4])
    plt.savefig(filename[:-4]+'.pdf', dpi=DPI, facecolor='w', edgecolor='w')
    F = plt.gcf()
    print F.get_size_inches()
    os.startfile(filename[:-4]+'.pdf')
 
 
def threshold():
    import os
    import matplotlib
    
    cdict = {'red':   [(0.0,  0.0, 0.0),
                   (0.5,  1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'green': [(0.0,  0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0,  1.0, 1.0)],

         'blue':  [(0.0,  0.0, 0.0),
                   (0.5,  0.0, 0.0),
                   (1.0,  1.0, 1.0)]}

    mcmap = plt.matplotlib.colors.LinearSegmentedColormap('three', cdict, N=5)
    procdir = 'F:\\IPYdata\\CBAND\\sections'
    filename = 'troll_I.bYY'
    os.chdir(procdir)
    TD = TGPR()
    TD.readBin(filename, 805)
    plt.imshow(TD.Tdata, aspect=3, cmap=mcmap, vmin=122, vmax=138)   
    plt.colorbar()
    plt.show()
    
if __name__ == '__main__':
    process()
#    merge()
#    PDFprint()
#    threshold()